Oa.controller('applyCtrl',function($scope,$http,$stateParams){
  //批复申请
  $scope.dealApply = function(id,result){
    $http.post(applyDeal_url,{
      dealId:id,
      dealStatus:true,
      dealResult:result
    })
    .success(function(res){
      if (res.code==200) {
        Notify(res.msg,"top-right");
        $scope.getMyNews();
      }else {
        Notify(res.data,"top-right");
      }
    })
    .error(function(){
      Notify("请求发送失败，请稍后重试","top-right");
    })
  }

  //获取我的申请单
  $scope.getMyApply = function(){
    $http.get(getMyApply_url,{
      params:{
        sender:$stateParams.id
      }
    })
    .success(function(res){
      if (res.code==200) {
    	 var idarr = [];  
        $scope.applications = res.data;
        for (var i = 0; i < res.data.length; i++) {
          var a = new Date(res.data[i].sendDate);
          $scope.applications[i].sendDate = a.toLocaleString();
          if (res.data[i].dealStatus && !res.data[i].reades) {
              idarr.push(parseInt(res.data[i].id));
            }
        }
        $scope.setRead(idarr);
        $scope.applications.reverse();
      }else {
        Notify(res.data,"top-right");
      }
    })
    .error(function(){
      Notify("获取失败，请稍后重试","top-right");
    });
  }

  //获取我的批复单
  $scope.getMyNews = function(){
    $http.get(getApplyByReceiver_url,{
      params:{
        receiver:$stateParams.id
      }
    })
    .success(function(res){
      if (res.code==200) {
        $scope.news = res.data;
        $scope.idarr = [];
        for (var i = 0; i < res.data.length; i++) {
          var a = new Date(res.data[i].sendDate);
          $scope.news[i].sendDate = a.toLocaleString();
          
        }
        $scope.news.reverse();
        
      }else {
        Notify(res.data,"top-right");
      }
    })
    .error(function(){
      Notify("获取失败，请稍后重试","top-right");
    });
  }

  //删除申请记录
  $scope.delNews = function(){
    var arr=[];
    var getSelectValueMenbers = $("input[name='delcheckbox']:checked").each(function(j) {
        if (j >= 0) {
          arr.push(parseInt($(this).val()));
        }
    });
    $http.post(delApply_url,{
      ids:arr
    })
    .success(function(res){
      if (res.code!=200) {
        Notify(res.data,"top-right");
      }else {
        $scope.getMyApply();
      }
    })
  }

  //显示申请详细信息
  $scope.showModal2 = false;
	$scope.toggleshowModal2 = function(){
		$scope.showModal2 = !$scope.showModal2;
	}
	$scope.showDetail = function(obj) {
    $scope.event = obj;
		$scope.showModal2 = true;
	}

	$scope.makesure = function(res){
		var text = res?"确认通过该申请?":"你确定驳回该申请?";
		bootbox.confirm(text, function (result) {
	          if (result) {
	              $scope.dealApply($scope.event.id,res);
	              $scope.showModal2 = !$scope.showModal2;
	              $scope.getMyNews();
	          }
	     });
	}

  //设置消息为已读
  $scope.setRead = function(arr){
	console.log("已读");
    $http.post(setApplyRead_url,{
      ids:arr,
      reades:1
    })
    .success(function(res){
    //  $scope.getMyApply();
      // $scope.hadNews();
    })
    .error(function(){
    	
    });
  }

  if($stateParams.type==1){$scope.getMyApply();}
  else if($stateParams.type==2){$scope.getMyNews();}
});
